/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainWin.java
 *
 * Created on 13.12.2009, 20:25:57
 */

package drivemachine;

import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultTreeCellRenderer;



/**
 *
 * @author positron
 */
public class MainWin extends javax.swing.JFrame {

	public static ImageIcon FOLDER_ICON, FOLDER_OPEN_ICON;
	public static  Scanner scanner, scanner2;
	private DriveTreeModel mod, mod2;
    /** Creates new form MainWin */
    public MainWin() {
		FOLDER_ICON = new ImageIcon( getClass().getResource("folder_close.png") );
		FOLDER_OPEN_ICON = new ImageIcon( getClass().getResource("folder_open.png") );
		scanner = new Scanner();
		scanner2 = new Scanner();
		mod = new DriveTreeModel();
		mod2 = new DriveTreeModel();
        initComponents();
		this.getRootPane().setDefaultButton( btScan );
		DefaultTreeCellRenderer rr = ((DefaultTreeCellRenderer)this.tree.getCellRenderer());
		rr.setLeafIcon(FOLDER_ICON);
		rr.setOpenIcon(FOLDER_OPEN_ICON);
		rr.setClosedIcon(FOLDER_ICON);
		rr = ((DefaultTreeCellRenderer)this.tree2.getCellRenderer());
		rr.setLeafIcon(FOLDER_ICON);
		rr.setOpenIcon(FOLDER_OPEN_ICON);
		rr.setClosedIcon(FOLDER_ICON);
		//getRootPane().setWindowDecorationStyle( JDialog);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fd = new javax.swing.JFileChooser();
        jScrollPane1 = new javax.swing.JScrollPane();
        tree = new javax.swing.JTree();
        btScan = new javax.swing.JButton();
        lbStat = new javax.swing.JLabel();
        btSave = new javax.swing.JButton();
        edPath = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tree2 = new javax.swing.JTree();
        btLoad = new javax.swing.JButton();
        btComp = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        fd.setCurrentDirectory(new java.io.File("."));

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("DriveMachine 2a");

        tree.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        tree.setModel(mod);
        tree.setLargeModel(true);
        jScrollPane1.setViewportView(tree);

        btScan.setText("Scan");
        btScan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btScanActionPerformed(evt);
            }
        });

        btSave.setText("Save");
        btSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSaveActionPerformed(evt);
            }
        });

        edPath.setText("E:/TMP");
        edPath.setToolTipText("");

        tree2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        tree2.setModel(mod2);
        jScrollPane2.setViewportView(tree2);

        btLoad.setText("Load");
        btLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLoadActionPerformed(evt);
            }
        });

        btComp.setText("Compare");
        btComp.setEnabled(false);
        btComp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCompActionPerformed(evt);
            }
        });

        jButton1.setText("Load");
        jButton1.setActionCommand("btLoad1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(edPath, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btScan)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btSave))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 346, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btLoad)
                        .addGap(195, 195, 195)
                        .addComponent(btComp))
                    .addComponent(jScrollPane2))
                .addContainerGap())
            .addComponent(lbStat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(edPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btScan)
                    .addComponent(btSave)
                    .addComponent(btComp)
                    .addComponent(btLoad)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
                    .addComponent(jScrollPane2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbStat, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void btScanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btScanActionPerformed
		//String root = edPath.getText();
		scanner.scan(edPath.getText());
		scanner.calcSize();
		lbStat.setText("Scan complete, "+scanner.getFolderCount()+" folders traversed");
		mod.update(scanner.getRoot());
	}//GEN-LAST:event_btScanActionPerformed

	private void btSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSaveActionPerformed
		if (fd.showSaveDialog(this) == JFileChooser.APPROVE_OPTION )
			try {
				scanner.save( fd.getSelectedFile().getAbsolutePath() );
				lbStat.setText("Save completed");
			} catch (IOException e) {
				JOptionPane.showMessageDialog(this, "There was an error while saving:"+e,
						"DriveMachine2 Error", JOptionPane.ERROR_MESSAGE);
			}

	}//GEN-LAST:event_btSaveActionPerformed

	private void btLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLoadActionPerformed
		if (fd.showOpenDialog(this) == JFileChooser.APPROVE_OPTION ) {
			//scanner.save(  );
			try {
				scanner2.load(fd.getSelectedFile().getAbsolutePath() );
				mod2.update(scanner2.getRoot());
				btComp.setEnabled(true);
			} catch(Exception e) {
				e.printStackTrace();
				 JOptionPane.showMessageDialog(this, "There was an error while loading:"+e,
						 "DriveMachine2 Error", JOptionPane.ERROR_MESSAGE);

			}
		}
	}//GEN-LAST:event_btLoadActionPerformed

	private void btCompActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCompActionPerformed
		Comparator cmp = new Comparator();
		cmp.compare(scanner2.getRoot(), scanner.getRoot());
		DiffWin dwin = new DiffWin(cmp);
		dwin.setLocationRelativeTo(this);
		dwin.setVisible(true);
		dwin.compModel.update(cmp.getRoot());
	}//GEN-LAST:event_btCompActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (fd.showOpenDialog(this) == JFileChooser.APPROVE_OPTION ) {
			//scanner.save(  );
			try {
				scanner.load(fd.getSelectedFile().getAbsolutePath() );
				mod.update(scanner.getRoot());
				//btComp.setEnabled(true);
			} catch(Exception e) {
				 JOptionPane.showMessageDialog(this, "There was an error while loading:"+e,
						 "DriveMachine2 Error", JOptionPane.ERROR_MESSAGE);

			}
		}
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btComp;
    private javax.swing.JButton btLoad;
    private javax.swing.JButton btSave;
    private javax.swing.JButton btScan;
    private javax.swing.JTextField edPath;
    private javax.swing.JFileChooser fd;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lbStat;
    private javax.swing.JTree tree;
    private javax.swing.JTree tree2;
    // End of variables declaration//GEN-END:variables

}
